/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.bean.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * this class represents the parameters need for adding text to an image.
 */
public class AddTextTransformation
{
  /**
   * default font size.
   */
  public static final float DEFAULT_FONT_SIZE = 12;

  /**
   * all fonts indexed by their name.
   */
  private static final Map<String, Font> FONTS_BY_NAME = new HashMap<String, Font>();



  /**
   * returns the {@link Font} corresponding to the name given in parameter.
   * 
   * @param fontName
   *          the name of the {@link Font}
   * @return the {@link Font} corresponding to the name, or <code>null</code>
   *         if no {@link Font} matches
   */
  private static Font getFont(String fontName)
  {
    final String indexableFontName = fontName.toLowerCase(Locale.getDefault());
    if (FONTS_BY_NAME.containsKey(indexableFontName)) {
      return FONTS_BY_NAME.get(indexableFontName);
    }
    Font res = null;
    for (Font font : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
      if (font.getName().toLowerCase().equals(indexableFontName)) {
        FONTS_BY_NAME.put(indexableFontName, font);
        return font;
      }
      if (res == null && font.getName().toLowerCase().contains(indexableFontName)) {
        res = font;
      }
    }
    FONTS_BY_NAME.put(indexableFontName, res);
    return res;
  }

  /**
   * the text to diplay.
   */
  private String text;

  /**
   * the font to use, <code>null</code> for the default font.
   */
  private Font font;

  /**
   * the font size to use, <code>null</code> for default font size.
   */
  private float fontSize = DEFAULT_FONT_SIZE;

  /**
   * the starting x position for drawing the text. <code>null</code> means
   * centering, positive or 0 means from the beginning to the image, negative
   * mean from the end of the image.
   */
  private Integer startingX;

  /**
   * the starting y position for drawing the text. <code>null</code> means
   * centering, positive or 0 means from the beginning to the image, negative
   * mean from the end of the image.
   */
  private Integer startingY;

  /**
   * the angle of the text in radian in trigonometric order (anti-clockwise), 0
   * or <code>null</code> means normal.
   */
  private Double angle;

  /**
   * the color of the text, <code>null</code> means solid black.
   */
  private Color color;



  /**
   * getter for the angle property.
   * 
   * @return the angle
   */
  public Double getAngle()
  {
    return angle;
  }



  /**
   * getter for the color property.
   * 
   * @return the color
   */
  public Color getColor()
  {
    return color;
  }



  /**
   * getter for the font property.
   * 
   * @return the font
   */
  public Font getFont()
  {
    return font;
  }



  /**
   * getter for the fontSize property.
   * 
   * @return the fontSize
   */
  public float getFontSize()
  {
    return fontSize;
  }



  /**
   * getter for the startingX property.
   * 
   * @return the startingX
   */
  public Integer getStartingX()
  {
    return startingX;
  }



  /**
   * getter for the startingY property.
   * 
   * @return the startingY
   */
  public Integer getStartingY()
  {
    return startingY;
  }



  /**
   * getter for the text property.
   * 
   * @return the text
   */
  public String getText()
  {
    return text;
  }



  /**
   * setter for the angle property.
   * 
   * @param angle
   *          the angle to set
   */
  public void setAngle(Double angle)
  {
    this.angle = angle;
  }



  /**
   * setter for the color property.
   * 
   * @param color
   *          the color to set
   */
  public void setColor(Color color)
  {
    this.color = color;
  }



  /**
   * setter for the font property.
   * 
   * @param font
   *          the font to set
   */
  public void setFont(Font font)
  {
    this.font = font;
  }



  /**
   * setter for the font property.
   * 
   * @param fontName
   *          the name of the font to set
   */
  public void setFont(String fontName)
  {
    this.font = getFont(fontName);
  }



  /**
   * setter for the fontSize property.
   * 
   * @param fontSize
   *          the fontSize to set
   */
  public void setFontSize(float fontSize)
  {
    this.fontSize = fontSize;
  }



  /**
   * setter for the startingX property.
   * 
   * @param startingX
   *          the startingX to set
   */
  public void setStartingX(Integer startingX)
  {
    this.startingX = startingX;
  }



  /**
   * setter for the startingY property.
   * 
   * @param startingY
   *          the startingY to set
   */
  public void setStartingY(Integer startingY)
  {
    this.startingY = startingY;
  }



  /**
   * setter for the text property.
   * 
   * @param text
   *          the text to set
   */
  public void setText(String text)
  {
    this.text = text;
  }
}
